package com.webserver1.core;

import com.webserver1.controller.UserController;
import com.webserver1.http.HttpServleResponse;
import com.webserver1.http.HttpServletRequest;

import java.io.File;
import java.net.URISyntaxException;

public class DispatcherServlet {
    private static File rootDir;
    private static File staticDir;
    static { /*不懂*/
        try {
            //roodDir表示类加载路径：target/classes目录
            rootDir = new File(
                    DispatcherServlet.class.getClassLoader().getResource(".").toURI()
            );
            //定位static目录
            staticDir = new File(rootDir,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    //处理请求，判断是否有业务要处理
    public void service(HttpServletRequest request, HttpServleResponse response){
        String path = request.getRequestURI();//获取抽象路径
        if ("/regUser".equals(path)){//处理业务

        }else {//回复页面
            File file = new File(staticDir,path);
            if (file.isFile()){
                //回复页面
                UserController controller = new UserController();
                controller.reg(request,response);

            }
        }
    }
}
